package cn.hdu.liang.generics;

interface Selector<T> {
    boolean end();

    T current();

    void next();
}

class Sequence<T> {
    private Object[] items;
    private int next;

    public Sequence(int size) {
        items = new Object[size];
    }

    public void add(T x) {
        if(next < items.length) {
            items[next++] = x;
        }
    }

    private class SequenceSelector implements Selector<T> {
        private int i;

        @Override
        public boolean end() {
            return i == items.length;
        }

        @Override
        public T current() {
            return (T) items[i];
        }

        @Override
        public void next() {
            if(i < items.length) {
                i++;
            }
        }
    }

    Selector<T> selector() {
        return new SequenceSelector();
    }
}

public class Exam4 {
    public static void main(String[] args) {
        Sequence<String> seq = new Sequence<>(10);
        for(int i = 0; i < 10; i++) {
            seq.add(Integer.toString(i));
        }
        Selector<String> stor = seq.selector();
        while(!stor.end()) {
            System.out.print(stor.current() + "  ");
            stor.next();
        }
    }
}
